package com.max.spark.utils

import java.util.Properties
import scala.collection.mutable

object PropsUtil {

  /**
   * 将参数封装成 properties
   * @param driver
   * @param username
   * @param password
   * @return
   */
  def getProps(driver: String, username: String, password: String): Properties = {
    // 添加属性
    val properties: Properties = ConfUtil.props
    ConfUtil.setProperty("user", username)
    ConfUtil.setProperty("password", password)
    ConfUtil.setProperty("driver", driver)
    properties
  }

  /**
   * 将参数封装成 map 形式
   * @param driver
   * @param hostname
   * @param port
   * @param username
   * @param password
   * @param db
   * @param table
   * @return
   */
  def getPropsByMap(protocol: String = "mysql", driver: String, hostname: String, port: String, username: String, password: String,
                   db: String, table: String): mutable.Map[String, String] ={
    val url = s"jdbc:$protocol://$hostname:$port/$db"
    val prop = scala.collection.mutable.Map[String,String]()
    prop.put("user", username)
    prop.put("password", password)
    prop.put("driver", driver)
    prop.put("dbtable", table)
    prop.put("url", s"$url?characterEncoding=utf8&serverTimezone=UTC")
    prop
  }

}
